/*
 * modbus.h
 *
 *  Created on: Jun 10, 2024
 *      Author: PIANO
 */

#ifndef COMPONENTS_MODBUS_H_
#define COMPONENTS_MODBUS_H_

/**
 * includes
 *
 *
 */
#include "uart.h"

/**
 * defines
 *
 */
//modbus function code define
#define READ_COIL_STATUS							01
#define READ_DISCRETE_INPUT							02
#define READ_HOLDING_REGISTER						03
#define READ_INPUT_REGISTER							04
#define WRITE_SINGLE_COIL							05
#define WRITE_SINGLE_REGISTER						06
#define WRITE_MULTIPLE_COIL							15
#define WRITE_MULTIPLE_REGISTER						16

/**
 * typedefs
 *
 */
typedef struct {

	uint8_t slaveAddress;
	uint8_t functionCode;
	uint16_t startAddress;
	uint16_t length;
	int16_t *pData;

} MODBUS_MasterDataTypeDef;

typedef struct {

	uint16_t address;

	bool *pCoilStatus;
	bool *pDiscreteInput;
	uint16_t *pHoldingRegister;
	uint16_t *pInputRegister;

	uint16_t coilCount;
	uint16_t discreteInputCount;
	uint16_t holdingRegisterCount;
	uint16_t inputRegisterCount;

} MODBUS_SlaveDataTypeDef;

/**
 * functions
 *
 */
void modbusMaster(UART_HandleTypeDef *huart, UART_DataTypeDef *duart,
		uint8_t slaveAddress, uint8_t functionCode, uint16_t startAddress,
		uint16_t length, uint16_t *pData);

void modbusSlave(UART_HandleTypeDef *huart, UART_DataTypeDef *duart,
		MODBUS_SlaveDataTypeDef *pSlaveData);

#endif /* __MODBUS_H_ */
